angular.module('app')
    .directive("folderTree", function() {
        return {
            restrict: "E",
            scope: {
                data: '='
            },
            templateUrl: 'tpl/tree/tree.html',
            controller: function ($scope,$q,toaster,utilService) {
                $scope.isCheck=function(item){
                    if(item.children.length==0){
                        selected=(item.isSelect==1);
                    }else{
                        var selectLength=0;
                        for(var i=0;i<item.children.length;i++){
                            if(item.children[i].isSelect==1){
                                selectLength++;
                            }
                        }
                        item.isSelect=(selectLength>0)?1:0;
                        selected=(selectLength>0);
                    }
                    return selected;
                }

                $scope.updateSelection=function($event,item){
                        var checkbox = $event.target ;
                        item.isSelect=checkbox.checked?1:0;
                        selectItem(item);
                }

                var selectItem=function(item){
                    for(var i=0;i<item.children.length;i++){
                        item.children[i].isSelect=item.isSelect;
                        if(item.children[i].children.length>0){
                            selectItem(item.children[i]);
                        }
                    }

                }




            }



    }})